<?php

namespace app\student\model;

use think\Db;
use think\Exception;

class teacherModel
{

    public static function selectTeacherModel()
    {
        $result = Db::table('s_teacher')
                        ->alias('t')->field("t.*,group_concat(c.name) as c_name")
                        ->leftjoin('s_teaching tea','t.id = tea.t_id')
                        ->leftJoin('s_course c','c.id = tea.c_id')
                        ->group('t.id')->select();
        return $result;
    }


    public static function insertTeacherModel(array $insert)
    {
        //这里我们需要把$insert 里面的属于 老师表和科目表的属性给分开
        $teaching['c_id'] = $insert['c_name'];
        unset($insert['c_name']);
        //var_dump($teaching);die;
        //我们添加老师表的时候，同样的也需要添加师科表，所以需要用到事务的概念
        //开启事务
        Db::startTrans();
        try {
//            $start = Db::table('s_teacher')->insert($insert);
//            Db::table('s_teacher')->getLastInsID();获取表里面添加的最后一条id
            //var_dump($start);die;
            //获取数据库中最后添加的一条id
            //echo Db::getLastSql();die;
            //$teaching['t_id']  = mysqli_insert_id();
            $teaching['t_id'] = Db::table('s_teacher')->insertGetId($insert);
            Db::table('s_teacher')->getLastInsID();
            //var_dump($teaching);die;
            if(empty($teaching['t_id']))
            {
                //回滚
                Db::rollback();
            }else{

                //进行循环，将$teaching表中的c_id 给分开
                foreach($teaching['c_id'] as $value)
                {
                    //var_dump($value);die;
                    //这里必须使用数组，不然传递过来的值会被覆盖
                    $teachingInsert[] = ['c_id'=>$value,'t_id'=>$teaching['t_id']];
                }

                $result = Db::table('s_teaching')->insertAll($teachingInsert);
                //echo Db::getLastSql(); die;
                if(empty($result))
                {
                    //回滚
                    Db::rollback();
                }else{
                    //提交
                    Db::commit();
                    return $result;
                }

            }
        }catch(Exception $exception)
        {
            //回滚
            Db::rollback();
        }

        //防止一直回滚
        return null;

    }

    //求出科目表中有多少个科目
    public static function selectCourse()
    {
        $result = Db::table('s_course')->select();
        return $result;
    }




    public static function deleteTeacherModel(array $where)
    {
        //先删除老师表里面的数据--->再删除师科表里面的数据---->在修改成绩表里面的数据
        //开启事务
        Db::startTrans();
        try {
            $teacher = Db::table('s_teacher')->where($where)->delete();
            //判断是否删除成功
            if(empty($teacher))
            {
                //回滚
                Db::rollback();
            }else{
                //这里时候，需要根据老师表来删除师科表了delete from s_teaching where t_id = 51
                $where_t_id['t_id'] = $where['id'];
                $teaching = Db::table('s_teaching')->where($where_t_id)->delete();
                if(empty($teaching))
                {
                    //回滚
                    Db::rollback();
                }else{
                    //提交
                    Db::commit();
                    return $teaching;
                }

//                if(empty($teaching))
//                {
//                    //回滚
//                    Db::rollback();
//                }else{
//                    //这里我们已经删除了老师表和师科表里面的数据，同样的我们现在需要修改的的就是成绩表里面的数据了
//                    //update s_report set t_id = 50 ,c_id = 2 where t_id = 47
//                    //取出了老师表的id
//                    $teacher_id = Db::table('s_teacher')->column('id');
//                    //从老师表中的id随机出来
//                    $report = rand($teacher_id);
//                }
            }
        }catch(Exception $exception){
            //回滚
            Db::rollback();
        }
        return null;

    }


    //查询出师科表里面的科目id
    public static function teachingModel(array $where)
    {
        $result = Db::table('s_teaching')->where($where)->select();
        return $result;
    }


    public static function GetTeacherModel(array $where)
    {

//        $result = Db::table('s_teacher')->get($where);
//        return $result;
        $where['t.id'] = $where['id'];
        unset($where['id']);
        //var_dump($where);die;
        $result = Db::table('s_teacher')
            ->alias('t')->field("t.*,group_concat(c.name) as c_name,group_concat(c.id) as c_id")
            ->leftjoin('s_teaching tea','t.id = tea.t_id')
            ->leftJoin('s_course c','c.id = tea.c_id')
            ->group('t.id')->get($where);
        return $result;
    }

    public static function updateTeacher(array $where)
    {
        //var_dump(1);die;
        //将$where 里面的老师id和科目id给传出来
        $teaching['c_id'] = $where['c_name'];
        $teaching['t_id'] = $where['id'];
        unset($where['c_name']);
       // var_dump($where,$teaching);
        //开启事务
        Db::startTrans();
        try {
            //修改，修改，必须要修改返回的才有值，不然的没有影响是不会有结果的
            $where_id['id'] = $where['id'];
            $start = Db::table('s_teacher')->where($where_id)->update($where);
            //var_dump($start);die;
            //这里取消判断$start是否存在，因为如果只是单纯的修改科目的话，老师表的其他属性不变，返回的是一个0值，所以去掉了

                //删除原本属于s_teaching表里面的值
                $delete = Db::table('s_teaching')->where('t_id',$where['id'])->delete();
                if(empty($delete))
                {
                    //回滚
                    Db::rollback();
                }else{
                    //删除后再进行添加

                    foreach ($teaching['c_id'] as $key=>$value)
                    {
                        $teachingInsert[] = ['c_id'=>$value,'t_id'=>$teaching['t_id']];
                    }
                    $insert = Db::table('s_teaching')->insertAll($teachingInsert);
                    if(empty($insert))
                    {
                        //回滚
                        Db::rollback();
                    }else{
                        //提交
                        Db::commit();
                        return $insert;
                    }

                }

        }catch (Exception $exception){
            //回滚
            Db::rollback();
        }
        return null;
    }
}